package com.hong.utils.executors;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: Administrator
 * @Date: 2018/7/30 16:57
 * @Description:
 */
public class ComplexExample {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
       /* final ExecutorService service = Executors.newFixedThreadPool(5);
        final CompletionService<Object> completionService = new ExecutorCompletionService<>(service);
        List<Runnable> tasks = IntStream.range(0,5).boxed().map(ComplexExample::toTask).collect(Collectors.toList());
        List<Future<?>> futureList = new ArrayList<>();
        tasks.forEach(r->futureList.add(service.submit(r)));
        Future<?> future;
        while ((future=completionService.take())!=null){
            System.out.println(future.get());
        }*/
       /* futureList.get(4).get();
        System.out.println("=======4========");
        futureList.get(3).get();
        System.out.println("=======3========");
        futureList.get(2).get();
        System.out.println("=======2========");
        futureList.get(1).get();
        System.out.println("=======1========");
        futureList.get(0).get();
        System.out.println("=======0========");*/
        /*final ExecutorService service = Executors.newSingleThreadExecutor();
        List<Runnable> tasks = IntStream.range(0,5).boxed().map(ComplexExample::toTask).collect(Collectors.toList());
        final CompletionService<Object> completionService = new ExecutorCompletionService<>(service);
        tasks.forEach(r->completionService.submit(Executors.callable(r)));

        TimeUnit.SECONDS.sleep(5);
        List<Runnable> runnables = service.shutdownNow();
        System.out.println(runnables);*/

        /*final ExecutorService service = Executors.newSingleThreadExecutor();
        List<Callable<Integer>> tasks = IntStream.range(0,5).boxed().map(MyTask::new).collect(Collectors.toList());
        final CompletionService<Object> completionService = new ExecutorCompletionService<>(service);

        tasks.forEach(completionService::submit);
        TimeUnit.SECONDS.sleep(12);
        service.shutdownNow();

        tasks.stream().filter(callable->!((MyTask)callable).isSuccess()).foreach(System.out::println);*/
    }

    private static class MyTask implements Callable<Integer>{

        private final Integer value;

        private static boolean success = false;

        MyTask(Integer value) {
            this.value = value;
        }

        @Override
        public Integer call() throws Exception {
            System.out.printf("The Task [%d] will be executed.\n",value);
            TimeUnit.SECONDS.sleep((value * 5 + 10));
            System.out.printf("The Task [%d] execute done.\n",value);
            success = true;
            return value;
        }

        public static boolean isSuccess(){
            return  success;
        }
    }

    private static  Runnable toTask(int i ){
        return ()->{

            try {
                System.out.printf("The Task [%d] will be executed .\n",i);
                TimeUnit.SECONDS.sleep(i*5+10);
                System.out.printf("The Task [%d] execute done.\n",i);
            } catch (InterruptedException e) {
                System.out.printf("The Task[%d] be interrupted.\n",i);
            }
        };
    }
}
